<template>
  <div class="settings-info">
    <el-form :model="ruleForm"
             :rules="rules"
             ref="ruleForm"
             label-width="70px"
             class="demo-ruleForm">
      <!-- 姓名 -->
      <el-form-item label="昵称"
                    prop="username">
        <el-input v-model="ruleForm.nickname"></el-input>
      </el-form-item>
      <!-- 性别 -->
      <el-form-item label="性别"
                    prop="sex">
        <el-radio-group v-model="ruleForm.sex">
          <el-radio :label="1">男</el-radio>
          <el-radio :label="0">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <!-- 地址 -->
      <el-form-item label="地址"
                    prop="city">
        <el-input v-model="ruleForm.city"></el-input>
      </el-form-item>
      <!-- 签名 -->
      <el-form-item label="签名"
                    prop="sign">
        <el-input type="textarea"
                  maxlength="60"
                  v-model="ruleForm.sign"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button class="sumbtn"
                   @click="submitForm('ruleForm')">修改</el-button>
        <el-button class="reset"
                   @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import { personInfo, updateInfo } from '@/api/user'
export default {
  data() {
    return {
      ruleForm: {
        username: '',
        sex: 1,
        sign: '',
        city: ''
      },
      rules: {
        username: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        sex: [{ required: true, message: '选择性别', trigger: 'blur' }],
        sign: [{ required: true, message: '签名不超过50个字符', trigger: 'blur' }],
        city: [{ required: true, message: '请输入地址', trigger: 'blur' }]
      }
    }
  },
  methods: {
    ...mapActions(['getUserInfo']),
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          
          updateInfo(this.ruleForm).then(res => {
            console.log(res)
            this.initPerson()   // 获取最新数据渲染表单
            this.getUserInfo()  // 修改vuex中userinfo数据
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    async initPerson() {
      let { data: res } = await personInfo()
      console.log(res.data)
      this.ruleForm = res.data
    }
  },
  created() {
    this.initPerson()
  }
}
</script>

<style lang="less" scoped>
.settings-info {
  margin-top: 20px;
  ::v-deep .el-form {
    .el-input,
    .el-textarea {
      width: 80%;
      .el-input__inner,
      .el-textarea__inner {
        &:focus {
          border: 1px solid #33c5c5;
        }
      }
    }
    /* 单选框 */
    .el-radio__label {
      color: #33c5c5;
    }
    /* 单选框选中时 */
    .is-checked {
      .el-radio__inner {
        border-color: #33c5c5;
        background: #33c5c5;
      }
    }

    .el-button {
      width: 90px;
      height: 40px;
      span {
        color: #fff;
      }
    }
    .sumbtn {
      background-color: #33c5c5;
    }
    .reset {
      border: 1px solid #33c5c5;
      span {
        color: #33c5c5;
      }
      &:hover {
        background-color: rgba(51, 197, 197, 0.2);
      }
    }
  }
}
</style>